package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.common.CustomException;
import cn.zcscloud.mall.mbg.mapper.UmsResourceCategoryMapper;
import cn.zcscloud.mall.mbg.model.UmsResource;
import cn.zcscloud.mall.mbg.model.UmsResourceCategory;
import cn.zcscloud.mall.service.UmsResourceCategoryService;
import cn.zcscloud.mall.service.UmsResourceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UmsResourceCategoryServiceImpl extends ServiceImpl<UmsResourceCategoryMapper, UmsResourceCategory> implements UmsResourceCategoryService {

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 根据ID删除后台资源分类
     *
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        //判断资源分类下是否包含资源，是的话，不能删除
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsResource::getCategoryId, id);
        if (umsResourceService.count(lqw) > 0) {
            throw new CustomException("该分类存在资源，不能删除");
        }
        //删除
        this.removeById(id);
    }
}
